package scrumtaculous.service;

import java.util.List;

import scrumtaculous.domain.Iteration;
import scrumtaculous.domain.IterationDao;
import scrumtaculous.domain.Project;
import scrumtaculous.domain.ProjectDao;

public class ProjectManagerImpl implements ProjectManager
{

   private ProjectDao projectDao = null;
   private IterationDao iterationDao = null;

   public void setProjectDao(ProjectDao projectDaoIn)
   {
      this.projectDao = projectDaoIn;
   }

   public void setIterationDao(IterationDao iterationDao)
   {
      this.iterationDao = iterationDao;
   }

   @SuppressWarnings("unchecked")
   public List<Project> find()
   {
      return projectDao.find();
   }

   @SuppressWarnings("unchecked")
   public List<Project> find(Project project)
   {
      return projectDao.find(project);
   }

   public Project find(Integer id)
   {
      return (Project) projectDao.findById(id);
   }

   public Project save(Project project)
   {
      boolean createBacklog = project.isNew();

      Project result = projectDao.save(project);

      if (createBacklog)
      {
         Iteration iteration = new Iteration();
         iteration.setName("backlog");
         iteration.setNumber(0);
         iteration.setProjectId(result.getId());

         iterationDao.save(iteration);
      }

      return result;
   }
}
